<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('合伙人类型')" prop="partnerType">
            <dict-select
              v-model="form.partnerType"
              dictionary="partner_type"
              :placeholder="$t('合伙人类型')"
              multiple
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('投资者性质')" prop="investorNature">
            <el-select v-model="form.investorNature" :placeholder="$t('投资者性质')" multiple>
              <el-option
                v-for="dict in investorNatureOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-col :span="24">
            <el-form-item :label="$t('投资者专业性')" prop="investorMajor">
              <dict-select
                v-model="form.investorMajor"
                dictionary="investor_majors"
                :placeholder="$t('投资者专业性')"
                multiple
              />
            </el-form-item>
          </el-col>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('校验文件')" prop="verifyFile">
            <treeselect v-model="form.verifyFile" :options="defaultDir" class="topSearchTree" :flat="true" :multiple="true" :placeholder="$t('启动前校验的附件')" :normalizer="checkFileNormalizer" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="fund:fundFileVerification" />
    </div>
  </div>
</template>

<script>
import { saveFundFileVerification, getFundFileVerification } from '@/api/fund/verification/fundFileVerification.js'
import { getDirByBelong } from '@/api/file/filefolder/folder'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  components: {
    Treeselect
  },
  data() {
    // var changeInvestorNature = (rule, value, callback) => {
    //   if (!value) {
    //     callback(new Error(this.$t('必填信息')))
    //   }
    //   checkInvestorNature(value, this.form.id).then(response => {
    //     if (response.data != null && response.code === 200 && response.data.length > 0) {
    //       if (this.form.id === response.data[0].id) {
    //        callback()
    //       } else {
    //         callback(new Error(this.$t('已存在该类型,不能重复添加!')))
    //       }
    //     } else {
    //       callback()
    //     }
    //   })
    // }
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        investorNature: [{ required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }],
        investorMajor: [{ required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }],
        partnerType: [{ required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }],
        verifyFile: [{ required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }]
      },
      // 该业务的默认附件类型
      defaultDir: [],
      investorNatureOptions: []
    }
  },
  created() {
    this.getDicts('investor_nature').then(response => {
      this.investorNatureOptions = response.data
    })
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getFundFileVerification(data.id).then(response => {
          this.form = response.data
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
      // 查询默认的文件夹
      this.getDefaultDir()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        partnerType: [],
        investorMajor: [],
        investorNature: [],
        verifyFile: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveFundFileVerification(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    },
    // 启动流程校验附件的格式化方法
    checkFileNormalizer(node) {
      if (node.children && node.children.length === 0) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    },
    // 查询该类型的默认文件夹
    getDefaultDir() {
      getDirByBelong('fund').then(response => {
        if (response.data !== null && response.code === 200 && response.data.length > 0) {
          this.defaultDir = []
          response.data.forEach(item => {
            if (item.id === 'fund') {
              this.defaultDir.push(item)
            }
          })
        }
      })
    }
  }

}
</script>
<style scoped>
.topSearchTree {

}
</style>
